package com.app.controller.backstage;

import com.app.model.AdminUserModel;
import com.app.model.ComRecruitersModel;
import com.app.model.UserModel;
import com.app.service.AdminUserService;
import com.app.service.ComRecruitersService;
import com.app.service.EnterpriseService;
import com.app.service.UserService;
import com.app.util.Result;
import com.app.util.SMSUtils;
import com.app.util.TokenUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@Controller
public class PersonnelController {

    @Autowired
    AdminUserService adminUserService;
    @Autowired
    ComRecruitersService comRecruitersService;
    @Autowired
    UserService userService;

    @GetMapping("/getPersonnelList")
    public Result<Object> getPersonnelList(
            int page,int limit,
            @RequestParam(required = false) String keyWord,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Integer sceneType,
            @RequestParam(required = false) Integer enrollId,
            HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel adminUser = adminUserService.getUserById(Integer.parseInt(userId));
        if(adminUser==null)
        {
            return Result.error(405,"用户不存在");
        }

        ComRecruitersModel model = new ComRecruitersModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        model.setKeyWord(keyWord);
        model.setStartTime(startTime);
        model.setEndTime(endTime);
        if(enrollId!=null)
        {
            model.setComId(enrollId);
        }
        UserModel user = new UserModel();
        user.setRRegisterState(sceneType);
        model.setUser(user);
        return Result.success(comRecruitersService.personnelList(model));
    }

    @GetMapping("/toExamineUser")
    public Result<Object> toExamineUser(
            @RequestParam(required = false) int id,
            @RequestParam(required = false) int stateType,
            @RequestParam(required = false) String remarks,
            HttpServletRequest request) throws IOException {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405,"token不存在");
        }

        AdminUserModel adminUser = adminUserService.getUserById(Integer.parseInt(userId));
        if(adminUser==null)
        {
            return Result.error(405,"用户不存在");
        }
        UserModel user = userService.getUserByRId(id);
        user.setRRegisterState(stateType);
        if(stateType==9)
        {
            user.setRemarks(null);
            SMSUtils.examine(user.getMobile());//发送短信通知
        }else{
            user.setRemarks(remarks);
        }

        userService.update(user);
        return Result.success("操作成功");
    }




}
